package com.qingfeng.client.api;

import com.alibaba.fastjson.JSONObject;
import com.qingfeng.client.bean.Buyer;
import com.qingfeng.client.bean.OrderStore;
import com.qingfeng.client.dao.BuyerMapper;
import com.qingfeng.client.dao.OrderStoreMapper;
import com.qingfeng.client.service.BuyerService;
import com.qingfeng.client.service.ShoppingCartService;
import com.qingfeng.util.ApiMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

/**
 * @Description:用户首页
 * @Author: 流氓
 * @Create: 2017-11-21-10:10
 * @Version:
 */
@Api(tags = "买家个人信息")
@Controller
@RequestMapping("/app/buyer")
public class AppBuyerApi extends BaseV1Api{



    @Autowired
    private BuyerService buyerService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Resource
    private OrderStoreMapper orderStoreMapper;

    @Resource
    private BuyerMapper buyerDAO;

    /**
     * 前端获取登陆信息接口
     * @return
     */
    @ApiOperation("获取用户信息")
    @GetMapping(value = "/info")
    @ResponseBody
    public ApiMsg baseInfo() {
        Buyer buyerBean = getUserByToken();
        System.out.println("=============获取用户信息=============== "+buyerBean);
        if (buyerBean==null){
            return error("用户未登录");
        }
        List list = orderStoreMapper.statNum(buyerBean.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("buyer",buyerBean);
        jsonObject.put("stat",list);
        return success(jsonObject);
    }

    @ApiOperation("更新用户个人信息")
    @PostMapping(value = "/update")
    @ResponseBody
    public ApiMsg update(@RequestBody Buyer buyer,HttpServletRequest request){
        Buyer buyerBean = (Buyer) request.getSession().getAttribute("buyerBean");
        if (buyerBean==null){
            return error("用户未登录");
        }else {
            buyer.setId(buyerBean.getId());
            buyerDAO.updateById(buyer);
            return success();
        }
    }

    /**
     * 用于测试的用户登陆，后期需要删除
     * @param session
     * @param phone
     * @param password
     * @return
     * @throws UnsupportedEncodingException
     */
//    @PostMapping(value = "/login")
//    @ResponseBody
//    public Object login(HttpSession session, @RequestParam String phone, @RequestParam String password) throws UnsupportedEncodingException {
//        Buyer buyerBean = (Buyer) session.getAttribute("buyerBean");
//        if (buyerBean==null) {
//            buyerBean = buyerService.login(phone, password);
//        }
//        if (buyerBean !=null ){
//            session.setAttribute("buyerBean",buyerBean);
//            return success("登录成功",buyerBean.getToken());
//        }
//        return error();
//    }



    /**
     * 用户退出登录
     *
     * @param session
     * @return
     */
    @ApiOperation("退出登录")
    @GetMapping(value = "/logout")
    @ResponseBody
    public Object logout( HttpSession session) {
        Buyer buyer = (Buyer) session.getAttribute("buyerBean");
        if (buyer != null) {
            session.invalidate();
        }
        return success();
    }

}
